/*************************************************************************
 * file name : MapUtil.java
 * owner : Ding Zhe
 * copyright : umpay
 * description : 
 * modified :  
 *************************************************************************/
package com.umpay.privi.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.umpay.privi.common.ReqMessage;
import com.umpay.privi.common.XmlData;

/*************************************************************************
 * class : MapUtil
 * @author : Ding Zhe
 * @version : 1.0  
 * description : 
 * @see :                         
 *************************************************************************/
public class MapUtil {
	public static LinkedHashMap<String, String> sort(Map<String, String> srcMap) {
		LinkedHashMap<String, String> resultMap = new LinkedHashMap<String, String>();
		// 通过ArrayList构造函数把map.entrySet()转换成list
		List<Map.Entry<String, String>> mappingList = new ArrayList<Map.Entry<String, String>>(
				srcMap.entrySet());
		// 通过比较器实现比较排序
		Collections.sort(mappingList,
				new Comparator<Map.Entry<String, String>>() {
					public int compare(Map.Entry<String, String> mapping1,
							Map.Entry<String, String> mapping2) {
						return mapping1.getKey().compareTo(mapping2.getKey());
					}
				});
		for (Map.Entry<String, String> mapping : mappingList) {
			resultMap.put(mapping.getKey(), mapping.getValue());
		}
		return resultMap;
	}
	
	public static String map2String(Map<String,String> map){
		StringBuilder sb = new StringBuilder();
		for(String key:map.keySet()){
			String tmp = map.get(key);
			sb.append(key).append(":").append(tmp).append(";");
		}
		return sb.toString();
	}
	
	/**
	 * desc: 重载map2String
	 * <p>创建人：Ding Zhe , Jul 11, 2012 3:28:47 PM</p>
	 * <p>将map转换为string时，过滤掉某些域</p>
	 * @param paraMap 参数map
	 * @param ignoreField 忽略的域，以逗号分隔
	 * @return map对应的string
	 */
	public static String map2String(Map<String,String> paraMap, String ignoreField ){
		StringBuilder sb = new StringBuilder();
		for(String key:paraMap.keySet()){
			if ( -1 == ignoreField.indexOf(key.trim())) {
				//key值不在忽略域字符串时，添加到打印sb中
				String tmp = paraMap.get(key);
				sb.append(key).append(":").append(tmp).append(";");
			}
		}
		return sb.toString();
	}
	
	/**
	 * desc:支持对ReqMessage的处理
	 * <p>创建人：Ding Zhe , Jul 11, 2012 3:52:44 PM</p>
	 * @param reqMessage 请求参数
	 * @param ignoreField 忽略的域
	 * @return
	 */
//	public static String reqMessage2String(ReqMessage reqMessage, String ignoreField ){
//		StringBuilder sb = new StringBuilder();
//		sb.append("rpid[").append(reqMessage.getRpid())
//		  .append("] reqDate[").append(reqMessage.getReqDate())
//		  .append("] reqTime[").append(reqMessage.getReqTime())
//		  .append("] reqMap[").append(map2String(reqMessage.getReqMap(), ignoreField)).append("]");
//		
//		return sb.toString();
//	}
	
	public static Map<String,String> string2Map(String str){
		Map<String,String>retMap = new HashMap<String, String>();
		if(null == str || str.indexOf(';')==-1)return retMap;
		String[] element = str.split(";");
		for(int i=0;i<element.length;i++){
			if(element[i].indexOf(':')==-1)continue;
			String[] kv = element[i].split(":");
			if(kv.length!=2)continue;
			retMap.put(kv[0], kv[1]);
		}
		return retMap;
	}

	public static void main(String[] args) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("month", "month");
		map.put("bread", "bread");
		map.put("attack", "attack");
		map.put(XmlData.PASSWD, "1234");
		map.put("newPassword", "123456");
//		Map<String, String> resultMap = sort(map);
//		System.out.println(resultMap);
//		String ignoreStr = "passWd,newPassword";
//		System.out.println(map2String(map, ignoreStr));
		ReqMessage reqMessage  = new ReqMessage();
		reqMessage.setRpid("001");
		reqMessage.setReqDate("20120711");
		reqMessage.setReqTime("155412");
//		reqMessage.setReqMap(map);
//		System.out.println(reqMessage2String(reqMessage, ignoreStr));
	}
}
